﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;


namespace LetsGoToSchool
{
    /// <summary>
    /// Interaction logic for Memory.xaml
    /// </summary>
    public partial class Memory : UserControl
    {
        private int _level;
        private String _theme;
        private MemoryGame _main;

        public Memory()
        {
            InitializeComponent();
            _theme = "food";

            EasyButton.Background = Brushes.DeepSkyBlue;
            MediumButton.Background = Brushes.Peru;
            HardButton.Background = Brushes.Peru;

            List<string> types = Database.getAllTypes();

            for (int i = 0; i < types.Count; i++)
            {
                // get all the images from the database
                List<string> list = Database.getAllMyImages(types[i]);

                if (list.Count >= 3)
                {
                    // make an item out of types[i] and add it to the surface list box

                    SurfaceListBoxItem item = new SurfaceListBoxItem();
                    item.Content = types[i];
                    item.Background = new SolidColorBrush(Color.FromArgb(0xB9, 0xAF, 0xB4, 0x00));
                    item.FontSize = 30;
                    item.FontWeight = FontWeights.Normal;
                    item.Width = 350;
                    item.HorizontalAlignment = HorizontalAlignment.Center;
                    item.VerticalAlignment = VerticalAlignment.Top;
                    item.HorizontalContentAlignment = HorizontalAlignment.Center;

                    _themes.Items.Add(item);
                }
            }

            //for (int i = 0; i < types.Count; i++)
            //{
            //    // make an item out of types[i] and add it to the surface list box

            //    SurfaceListBoxItem item = new SurfaceListBoxItem();
            //    item.Content = types[i];
            //    item.Background = new SolidColorBrush(Color.FromArgb(0xB9,0xAF,0xB4,0x00));
            //    item.FontSize = 30;
            //    item.FontWeight = FontWeights.Normal;
            //    item.Width = 350;
            //    item.HorizontalAlignment = HorizontalAlignment.Center;
            //    item.VerticalAlignment = VerticalAlignment.Top;
            //    item.HorizontalContentAlignment = HorizontalAlignment.Center;

            //    _themes.Items.Add(item);
            //}
            
        }
        /// <summary>
        /// Gets the icon file name
        /// </summary>
        public static String getIconFileName()
        {
            return "Icon-Memory.png";
        }

        /// <summary>
        /// Called when the easy button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EasyButton_PreviewContactTapGesture(object sender, ContactEventArgs e)
        {
            EasyButton.Background = Brushes.DeepSkyBlue;
            MediumButton.Background = Brushes.Peru;
            HardButton.Background = Brushes.Peru;
            _level = 1;
            _main.setLevel(1);
            
        }

        /// <summary>
        /// Called when the medium button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MediumButton_PreviewContactTapGesture(object sender, ContactEventArgs e)
        {
            _level = 2;
            _main.setLevel(2);
            //_main.setUpLevel2();
            EasyButton.Background = Brushes.Peru;
            MediumButton.Background = Brushes.DeepSkyBlue;
            HardButton.Background = Brushes.Peru;
        }

        /// <summary>
        /// Called when the Hard button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HardButton_PreviewContactTapGesture(object sender, ContactEventArgs e)
        {
            _level = 3;
            _main.setLevel(3);
            //_main.setUpLevel3();
            EasyButton.Background = Brushes.Peru;
            MediumButton.Background = Brushes.Peru;
            HardButton.Background = Brushes.DeepSkyBlue;
        }

        /// <summary>
        /// Called when the food theme is selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Food_PreviewContactTapGesture(object sender, ContactEventArgs e)
        {
            _theme = "food";
        }

        /// <summary>
        /// Called when the animals theme is selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Animals_PreviewContactTapGesture(object sender, ContactEventArgs e)
        {
            _theme = "animals";
        }

        /// <summary>
        /// Called when the sports theme is selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sports_PreviewContactTapGesture(object sender, ContactEventArgs e)
        {
            _theme = "sports";
        }

        /// <summary>
        /// Called when the quit button is clicked - closes the game
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _quitButton_Click(object sender, RoutedEventArgs e)
        {
            _main.Close();
        }

        /// <summary>
        /// Sets the reference to the MemoryGame object to the one passed in
        /// </summary>
        /// <param name="game"></param>
        public void setMain(MemoryGame game)
        {
            _main = game;
        }

        /// <summary>
        /// Returns the level of difficulty
        /// </summary>
        public int getLevel()
        {
            return _level;
        }

        /// <summary>
        /// Called when the start button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _startButton_Click(object sender, RoutedEventArgs e)
        {
            _main.startGame();
            this.Visibility = Visibility.Hidden;
            _main.Visibility = Visibility.Visible;

            // calls set up on the main object based on the level selected
            // if no level is selected, level 1 is chosen by default
            if (_level == 3)
            {
                _main.setUpLevel3(_theme);
            }
            else if (_level == 2)
            {
                _main.setUpLevel2(_theme);
            }
            else
            {
                _main.setUpLevel1(_theme);
            }
        }

        /// <summary>
        /// Called when the Easy button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param
        private void EasyButton_Click(object sender, RoutedEventArgs e)
        {
            EasyButton.Background = Brushes.DeepSkyBlue;
            MediumButton.Background = Brushes.Peru;
            HardButton.Background = Brushes.Peru;
            _level = 1;
            _main.setLevel(1);
        }


        private void _themes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_themes.SelectedIndex != -1) // if something is selected
            {
                _theme = ((SurfaceListBoxItem)(_themes.SelectedItem)).Content.ToString();
            }
        }

    }
}
